﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProductManager
{
    public partial class AddEditProduct : Form
    {
        private MainForm _mainForm = null;
        private DataTable _categoryList;

        public AddEditProduct(MainForm mainForm)
        {
            _mainForm = mainForm;
            InitializeComponent();
        }

        public void InitAdd()
        {
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnAddProductProvider.Enabled = false;
            btnDeleteProductProvider.Enabled = false;
        }

        public bool InitEdit(string id)
        {
            btnAdd.Enabled = false;
            btnFinishAddProduct.Visible = false;
            txtID.Text = id;
            DataTable product = DBAccess.GetRowWithID("Product", id);
            if (product != null && product.Rows.Count > 0)
            {
                txtName.Text = product.Rows[0]["Name"].ToString();
                txtDescription.Text = product.Rows[0]["Description"].ToString();
                RefreshProductProvider();
                SearchAllProvider();
            }
            else
            {
                return false;
            }
            return true;
        }

        private void RefreshProductProvider()
        {
            DataTable providers = DBAccess.GetProductProvider(txtID.Text);
            if (providers != null)
            {
                lvProductProvider.Items.Clear();
                foreach (DataRow row in providers.Rows)
                {
                    lvProductProvider.Items.Add(new ListViewItem(new string[] {
                        row["Name"].ToString(), row["Price"].ToString(), row["ID"].ToString() }));
                }
            }
        }

        private void SearchAllProvider()
        {
            DataTable products = DBAccess.Search("Provider", txtSearch.Text, "Name");
            if (products == null)
            {
                return;
            }
            lvAllProvider.Items.Clear();
            foreach (DataRow row in products.Rows)
            {
                lvAllProvider.Items.Add(new ListViewItem(new string[] {
                    row["Name"].ToString(), row["ID"].ToString() }));
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtName.Text != "" && txtDescription.Text != "" && cboCategory.Text != "")
            {
                if (DBAccess.AddProduct(txtName.Text, txtDescription.Text))
                {
                    if (cboCategory.SelectedItem.ToString() != "/")
                    {
                        DBAccess.AddProductCategory(DBAccess.GetLastRow("Product").Rows[0]["ID"].ToString(),
                            _categoryList.Rows[cboCategory.SelectedIndex - 1]["ID"].ToString());
                    }
                    MessageBox.Show("Add product successful!!!");
                    _mainForm.SearchProduct(true);
                    txtID.Text = DBAccess.GetLastRow("Product").Rows[0]["ID"].ToString();
                    btnUpdate.Enabled = true;
                    btnDelete.Enabled = true;
                    btnAddProductProvider.Enabled = true;
                    btnDeleteProductProvider.Enabled = true;
                    RefreshProductProvider();
                    SearchAllProvider();
                }
                else
                {
                    MessageBox.Show("Add product failed!!!");
                }
            }
            else
                MessageBox.Show("Give full info for this product");
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (DBAccess.UpdateProduct(txtID.Text, txtName.Text, txtDescription.Text))
            {
                if (cboCategory.SelectedItem.ToString() != "/")
                {
                    DBAccess.AddProductCategory(DBAccess.GetLastRow("Product").Rows[0]["ID"].ToString(),
                        _categoryList.Rows[cboCategory.SelectedIndex - 1]["ID"].ToString());
                }
                MessageBox.Show("Update product successful!!!");
                _mainForm.SearchProduct(true);
            }
            else
            {
                MessageBox.Show("Update product failed!!!");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to delete " + txtName.Text, "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (DBAccess.DeleteProduct(txtID.Text))
                {
                    MessageBox.Show("Delete product successful!!!");
                    _mainForm.SearchProduct(true);
                }
                else
                {
                    MessageBox.Show("Delete product failed!!!");
                }
                Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAddProductProvider_Click(object sender, EventArgs e)
        {
            if (lvAllProvider.SelectedItems.Count == 1)
            {
                DBAccess.AddProductProvider(
                    txtID.Text, lvAllProvider.SelectedItems[0].SubItems[1].Text, txtPrice.Text);
                RefreshProductProvider();
                txtPrice.Text = "";
            }
            else
            {
                MessageBox.Show("Please choose one provider!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDeleteProductProvider_Click(object sender, EventArgs e)
        {
            if (lvProductProvider.SelectedItems.Count == 1)
            {
                if (DBAccess.DeleteProductProvider(txtID.Text, lvProductProvider.SelectedItems[0].SubItems[2].Text))
                {
                    RefreshProductProvider();
                }
                else
                {
                    MessageBox.Show("Delete provider failed!!!");
                }
            }
            else
            {
                MessageBox.Show("Please choose one provider!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAddProvider_Click(object sender, EventArgs e)
        {
            AddEditProvider frmNew = new AddEditProvider();
            frmNew.InitAdd();
            frmNew.ShowDialog();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            SearchAllProvider();
        }

        private void AddEditProduct_Load(object sender, EventArgs e)
        {
            _categoryList = DBAccess.GetAllCategory();
            cboCategory.Items.Add("/");
            foreach (DataRow row in _categoryList.Rows)
            {
                cboCategory.Items.Add(row["Name"]);
            }
            cboCategory.SelectedIndex = 0;
        }

        private void lvAllProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvAllProvider.SelectedItems.Count == 1)
            {
                txtProvider.Text = lvAllProvider.SelectedItems[0].Text;
            }
        }

        private void lvAllProvider_DoubleClick(object sender, EventArgs e)
        {
            if (lvAllProvider.SelectedItems.Count == 1)
            {
                AddEditProvider frmNew = new AddEditProvider();
                if (frmNew.InitEdit(lvAllProvider.SelectedItems[0].SubItems[1].Text))
                {
                    frmNew.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("Please choose one provider!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lvProductProvider_DoubleClick(object sender, EventArgs e)
        {
            if (lvProductProvider.SelectedItems.Count == 1)
            {
                AddEditProvider frmNew = new AddEditProvider();
                if (frmNew.InitEdit(lvProductProvider.SelectedItems[0].SubItems[2].Text))
                {
                    frmNew.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("Please choose one provider!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnFinishAddProduct_Click(object sender, EventArgs e)
        {
            InitAdd();
            txtID.Text = ""; txtDescription.Text = ""; txtName.Text = "";
            cboCategory.Text = "";
            lvProductProvider.Items.Clear();
            lvAllProvider.Items.Clear();
        }
    }
}
